package com.eascs.dubbo.loan.service;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.common.service.ILoanInfoService;
import com.eascs.admin.common.vo.LoanApplyEntity;
import com.eascs.admin.common.vo.LoanEntity;
import com.eascs.admin.common.vo.LoanInfo;
import com.eascs.admin.common.vo.LoanUserInfo;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.admin.common.vo.O2oUpdateLoanExpireTimeEntity;
import com.eascs.admin.common.vo.WeChatParams;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.app.dao.DaoException;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;

/**
 * 
 * @author YCZ
 *
 */
@Service
public class LoanInfoServiceImpl implements ILoanInfoService{
	private final Logger logger = LoggerFactory.getLogger(LoanInfoServiceImpl.class);
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	@Override
	public com.eascs.admin.common.vo.LoanEntity queryLoanCount(LoanInfo loanInfo) {
		LoanEntity loanEntity=new LoanEntity();
		if(loanInfo==null){
			loanEntity.setErrorMsg("参数异常");
			loanEntity.setFlag(false);
		}else{
			logger.info("LoanInfo参数："+loanInfo.toString());
			if(StringUtils.isBlank(loanInfo.getDocumentNo())){
				loanEntity.setErrorMsg("身份证号不能为空");
				loanEntity.setFlag(false);
			}else if(StringUtils.isBlank(loanInfo.getLoanId())){
				loanEntity.setErrorMsg("客户ID不能为空");
				loanEntity.setFlag(false);
			}else if (StringUtils.isBlank(loanInfo.getDocumentType())) {
				loanEntity.setErrorMsg("证件类型不能为空");
				loanEntity.setFlag(false);
			}else{
				try {
					LoanEntity	loan=o2oloanapplyService.queryLoanCount(loanInfo);
					loan.setFlag(true);
					loan.setCount(loan.getCount());
					logger.info("dubbo调用成功");
				} catch (Exception e) {
					logger.error("dubbo异常："+e);
					loanEntity.setErrorMsg(e.getMessage());
					loanEntity.setFlag(false);
				}
			}
		}
		return loanEntity;
	}

	@Override
	public ModelResult<Boolean> isEditInfo(String loan_id) {
		logger.debug("调用接口成功，参数（用户id）：【{}】", loan_id);
		ModelResult<Boolean> model = new ModelResult<>();
		Boolean r = true;
		int count = o2oloanapplyService.findIsUserEditInfo(loan_id);
		logger.debug("查询数据库库返回数量：【{}】", count);
		if (count > 0) {
			r = false;
			model.withError("no can edit user info", "该用户存在贷款单号正在流程中，不能编辑客户信息");
		} else {
			model.withModel(r);
		}
		return model;
	}

	@Override
	public ModelResult<Boolean> updateLoanExpireTime(O2oUpdateLoanExpireTimeEntity updateEntity) {
		logger.debug("调用接口成功，参数：【{}】", updateEntity);
		ModelResult<Boolean> model = new ModelResult<>();
		try {
			String sql = "update o2o_loanapply ol set ol.end_bank_date=? where ol.loanapply_no=?";
			int i = o2oloanapplyService.updateSql(sql, updateEntity.getExpireTime(), updateEntity.getLoanapplyNo());
			if (i > 0) {
				model.withModel(true);
			} else {
				logger.error("更新主表贷款到期时间失败,失败原因:{}", new DaoException("update o2o_loanapply failed"));
				model.withError("update fail", "更新到期时间失败");
			}
		} catch (Exception e) {
			logger.error("更新主表贷款到期时间失败,失败原因:{}", e.getMessage());
			model.withError("update fail", "更新到期时间失败");
		}
		return model;
	}

	@Override
	public ModelResult<LoanApplyEntity> getLoanapplyInfo(String loanapplyNo,String bllCode) {
		ModelResult<LoanApplyEntity> model = new ModelResult<>();
		LoanApplyEntity loanApplyEntity = new LoanApplyEntity();
		LoanUserInfo loanUserInfo = new LoanUserInfo();
		O2oLoanapplyEntity o2oLoanapplyEntity = null;
		if(org.apache.commons.lang3.StringUtils.isNotBlank(loanapplyNo))
			o2oLoanapplyEntity = o2oloanapplyService.findById(loanapplyNo);
		if(org.apache.commons.lang3.StringUtils.isNotBlank(bllCode))
			o2oLoanapplyEntity = o2oloanapplyService.fingByBillCode(bllCode);
		
		if(o2oLoanapplyEntity == null){
			model.withModel(null);
			return model;
		}
		
		loanapplyNo = o2oLoanapplyEntity.getLoanapplyNo();
		if(o2oLoanapplyEntity!=null&&!StringUtils.isEmpty(o2oLoanapplyEntity.getLoanapplyNo())){
			O2oLoanUserEntity o2oLoanUserEntity = o2oLoanUserService.findById(o2oLoanapplyEntity.getLoanId(), loanapplyNo);
			O2oZxEntity o2oZxEntity=o2oZxServiceImpl.getEntityById(o2oLoanUserEntity.getZxNo());
			loanApplyEntity.setLoanapplyNo(o2oLoanapplyEntity.getLoanapplyNo());
			loanApplyEntity.setBillCode(o2oLoanapplyEntity.getBillCode());
			loanApplyEntity.setCreatRo(o2oLoanapplyEntity.getCreatRo());
			loanApplyEntity.setCreateDate(o2oLoanapplyEntity.getCreateDate());
			loanApplyEntity.setSubmitRo(o2oLoanapplyEntity.getSubmitRo());
			loanApplyEntity.setSubmitDate(o2oLoanapplyEntity.getSubmitDate());
			loanApplyEntity.setEndRo(o2oLoanapplyEntity.getEndRo());
			loanApplyEntity.setEndDate(o2oLoanapplyEntity.getEndDate());
			loanApplyEntity.setIslock(o2oLoanapplyEntity.getIslock());
			loanApplyEntity.setLoanaMount(o2oLoanapplyEntity.getLoanaMount());
			loanApplyEntity.setLoanTerm(o2oLoanapplyEntity.getLoanTerm());
			loanApplyEntity.setRepayMentway(o2oLoanapplyEntity.getRepayMentway());
			loanApplyEntity.setGuarAnteeWay(o2oLoanapplyEntity.getGuarAnteeWay());
			loanApplyEntity.setRepaymentSource(o2oLoanapplyEntity.getRepaymentSource());
			loanApplyEntity.setCustomerNo(o2oLoanapplyEntity.getCustomerNo());
			loanApplyEntity.setCustomerCode(o2oLoanapplyEntity.getCustomerCode());
			loanApplyEntity.setCustomeRname(o2oLoanapplyEntity.getCustomeRname());
			loanApplyEntity.setBankAccount(o2oLoanapplyEntity.getBankAccount());
			loanApplyEntity.setOperatorNo(o2oLoanapplyEntity.getOperatorNo());
			loanApplyEntity.setOperatorName(o2oLoanapplyEntity.getOperatorName());
			loanApplyEntity.setLoanrate(o2oLoanapplyEntity.getLoanrate());
			loanApplyEntity.setLtd(o2oLoanapplyEntity.getLtd());
			loanApplyEntity.setLtdCode(o2oLoanapplyEntity.getLtdCode());
			loanApplyEntity.setLtdName(o2oLoanapplyEntity.getLtdName());
			loanApplyEntity.setBankStatus(o2oLoanapplyEntity.getBankStatus());
			loanApplyEntity.setBankApplyno(o2oLoanapplyEntity.getBankApplyno());
			loanApplyEntity.setManagefeerate(o2oLoanapplyEntity.getManagefeerate());
			loanApplyEntity.setDepositrate(o2oLoanapplyEntity.getDepositrate());
			loanApplyEntity.setEaCooperationDate(o2oLoanapplyEntity.getEaCooperationDate());
			loanApplyEntity.setEaPurchaseamount(o2oLoanapplyEntity.getEaPurchaseamount());
			loanApplyEntity.setCustomerSalesamount(o2oLoanapplyEntity.getCustomerSalesamount());
			if (o2oZxEntity!=null) {
				loanApplyEntity.setBankType(o2oZxEntity.getZxBank());	
			}
			loanApplyEntity.setDataSource(o2oLoanapplyEntity.getDataSource());
			loanApplyEntity.setInstrumentNumber(o2oLoanapplyEntity.getInstrumentNumber());
			loanApplyEntity.setIsrenew(o2oLoanapplyEntity.getIsrenew());
			loanApplyEntity.setBankOsn(o2oLoanapplyEntity.getBankOsn());
			loanApplyEntity.setReceiveBank(o2oLoanapplyEntity.getReceiveBank());
			loanApplyEntity.setCreditline(o2oLoanapplyEntity.getCreditline());
			loanApplyEntity.setCreditRange(o2oLoanapplyEntity.getCreditRange());
			loanApplyEntity.setLoanFlg(o2oLoanapplyEntity.getLoanFlg());
			loanApplyEntity.setBankActno(o2oLoanapplyEntity.getBankActno());
			loanApplyEntity.setBankFlg(o2oLoanapplyEntity.getBankFlg());
			loanApplyEntity.setCustomerType(o2oLoanapplyEntity.getCustomerType());
			loanApplyEntity.setLoanType(o2oLoanapplyEntity.getLoanType());
			loanApplyEntity.setPayDate(o2oLoanapplyEntity.getPayDate());
			loanApplyEntity.setLoanapplynoMain(o2oLoanapplyEntity.getLoanapplynoMain());
			loanApplyEntity.setRemark(o2oLoanapplyEntity.getRemark());
			loanApplyEntity.setLoanId(o2oLoanapplyEntity.getLoanId());
			loanApplyEntity.setWfType(o2oLoanapplyEntity.getWfType());
			loanApplyEntity.setEaStockNum(o2oLoanapplyEntity.getEaStockNum());
			loanApplyEntity.setEaReturnedAmount(o2oLoanapplyEntity.getEaReturnedAmount());
			loanApplyEntity.setEaStockBorrowing(o2oLoanapplyEntity.getEaStockBorrowing());
			loanApplyEntity.setBankAmount(o2oLoanapplyEntity.getBankAmount());
			loanApplyEntity.setBankLoanName(o2oLoanapplyEntity.getBankLoanName());
			loanApplyEntity.setRisk(o2oLoanapplyEntity.getRisk());
			loanApplyEntity.setEaMount(o2oLoanapplyEntity.getEaMount());
			loanApplyEntity.setPaymentType(o2oLoanapplyEntity.getPaymentType());
			loanApplyEntity.setGeneralRate(o2oLoanapplyEntity.getGeneralRate());
			loanApplyEntity.setPaymentTime(o2oLoanapplyEntity.getPaymentTime());
			loanApplyEntity.setContractFlag(o2oLoanapplyEntity.getContractFlag());
			loanApplyEntity.setOpenAccountBank(o2oLoanapplyEntity.getOpenAccountBank());
			loanApplyEntity.setReferrerNo(o2oLoanapplyEntity.getReferrerNo());
			loanApplyEntity.setReferrerName(o2oLoanapplyEntity.getReferrerName());
			loanApplyEntity.setBcFlag(o2oLoanapplyEntity.getBcFlag());
			loanApplyEntity.setIsCheckMqCl(o2oLoanapplyEntity.getIsCheckMqCl());
			loanApplyEntity.setIsBankLoanAmount(o2oLoanapplyEntity.getIsBankLoanAmount());
			loanApplyEntity.setIsPlaceOnFile(o2oLoanapplyEntity.getIsPlaceOnFile());
			loanApplyEntity.setPlaceOnFileTime(o2oLoanapplyEntity.getPlaceOnFileTime());
			
			if(o2oLoanUserEntity!=null&&!StringUtils.isEmpty(o2oLoanUserEntity.getId())){
				loanUserInfo.setId(o2oLoanUserEntity.getId());
				loanUserInfo.setLoanId(o2oLoanUserEntity.getLoanId());
				loanUserInfo.setLoanappNo(o2oLoanUserEntity.getLoanappNo());
				loanUserInfo.setUserSurName(o2oLoanUserEntity.getUserSurName());
				loanUserInfo.setUserName(o2oLoanUserEntity.getUserName());
				loanUserInfo.setUserFullName(o2oLoanUserEntity.getUserFullName());
				loanUserInfo.setUserNamePinyin(o2oLoanUserEntity.getUserNamePinyin());
				loanUserInfo.setDocumentType(o2oLoanUserEntity.getDocumentType());
				loanUserInfo.setDocumentNo(o2oLoanUserEntity.getDocumentNo());
				loanUserInfo.setUserPhone(o2oLoanUserEntity.getUserPhone());
				loanUserInfo.setCustomerName(o2oLoanUserEntity.getCustomerName());
				loanUserInfo.setCustomerType(o2oLoanUserEntity.getCustomerType());
				loanUserInfo.setSex(o2oLoanUserEntity.getSex());
				loanUserInfo.setSpouseId(o2oLoanUserEntity.getSpouseId());
				loanUserInfo.setBankStatus(o2oLoanUserEntity.getBankStatus());
				loanUserInfo.setBankApplyno(o2oLoanUserEntity.getBankApplyno());
				loanUserInfo.setNotation(o2oLoanUserEntity.getNotation());
				loanUserInfo.setType(o2oLoanUserEntity.getType());
				loanUserInfo.setBackTime(o2oLoanUserEntity.getBackTime());
				loanApplyEntity.setLoanUserInfo(loanUserInfo);
			}
		}
		model.withModel(loanApplyEntity);
		return model;
	}

	@Override
	public ModelResult<String> getLoanApplyStatus(WeChatParams params) {
		ModelResult<String> model = new ModelResult<>();
		if (StringUtils.isEmpty(params.getIdCard())) {
			model.withError("must idcard not null", "身份证号不能为空");
			return model;
		}
		String tfType = o2oloanapplyService.findLoanApply(params);
		if (!StringUtils.isEmpty(tfType)) {
			model.withModel(tfType);
		} else {
			model.withError("error_msg", "无贷款单号");
		}
		return model;
	}

	@Override
	public ModelResult<LoanApplyEntity> getLoanapplyInfo(String loanapplyNo) {
		return getLoanapplyInfo(loanapplyNo,null);
	}

}
